Skip to content

sparse: add package for walking a sparse file's data extents - #27

Open
bradfitz wants to merge 1 commit into
mainfrom
bradfitz/sparse
Open

bradfitz wants to merge 1 commit into
mainfrom
bradfitz/sparse

Conversation

@bradfitz

@bradfitz bradfitz commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

Add a package that lists a file's data extents, so callers can skip
its holes.

This is the third time we've needed this in the corp repo. The first,
cmd/cihostlet/sparse.go in tailscale/corp#38151, copied VM disk images
without materializing their holes, and was since deleted in
tailscale/corp#42869. The second, the ci/sparse package in
tailscale/corp#39248, hashed sparse ext4 images for build caching, and
never landed (yet?). The third, the Tart image peer fill in
tailscale/corp#52161, is a WIP with its own extent loop to send
disk.img without its holes, and could use this instead. So put it here
once.

It is the ci/sparse package from tailscale/corp#39248 with these
changes:

  • It works on macOS and FreeBSD as well as Linux. ci/sparse was
    Linux-only and hard-coded Linux's SEEK_DATA and SEEK_HOLE values,
    which are swapped on macOS; these come from x/sys/unix now.
  • It works on Windows too, with FSCTL_QUERY_ALLOCATED_RANGES, for the
    day cihostlet runs there. Only NTFS files marked sparse have holes;
    other files are one extent.
  • Where the file system can't report holes (EINVAL), or on other
    platforms, the rest of the file is reported as one extent, as both
    earlier versions did, so ForEachExtent is always correct, just not
    always sparse.
  • ForEachExtent now sets the file position to each extent's start, as
    its doc already promised, and clips extents to the given size.
  • Extent gets an End method.
  • It has tests, and an example of streaming a file with its holes,
    the shape that tailscale/corp#52161 needs.

The tests pass on Linux, macOS (APFS), FreeBSD 15.0 (UFS), and Windows
Server 2022 (NTFS).

Updates tailscale/corp#51142

Co-authored-by: Sam Wronski samw@tailscale.com

@bradfitz
bradfitz requested review from a team and runewake2 November 25, 2026 00:44
Add a package that lists a file's data extents, so callers can skip
its holes.

This is the third time we've needed this in the corp repo. The first,
cmd/cihostlet/sparse.go in tailscale/corp#38151, copied VM disk images
without materializing their holes, and was since deleted in
tailscale/corp#42869. The second, the ci/sparse package in
tailscale/corp#39248, hashed sparse ext4 images for build caching, and
never landed (yet?). The third, the Tart image peer fill in
tailscale/corp#52161, is a WIP with its own extent loop to send
disk.img without its holes, and could use this instead. So put it here
once.

It is the ci/sparse package from tailscale/corp#39248 with these
changes:

  - It works on macOS and FreeBSD as well as Linux. ci/sparse was
    Linux-only and hard-coded Linux's SEEK_DATA and SEEK_HOLE values,
    which are swapped on macOS; these come from x/sys/unix now.
  - It works on Windows too, with FSCTL_QUERY_ALLOCATED_RANGES, for the
    day cihostlet runs there. Only NTFS files marked sparse have holes;
    other files are one extent.
  - Where the file system can't report holes (EINVAL), or on other
    platforms, the rest of the file is reported as one extent, as both
    earlier versions did, so ForEachExtent is always correct, just not
    always sparse.
  - ForEachExtent now sets the file position to each extent's start, as
    its doc already promised, and clips extents to the given size.
  - Extent gets an End method.
  - FileHash is gone. It hashed only the data extents, as a build cache
    key that depends on where the file system put the holes, which is
    specific to that one caller; a caller that wants it can build it on
    ForEachExtent in a few lines.
  - It has tests, and an example of streaming a file with its holes,
    the shape that tailscale/corp#52161 needs.

The tests pass on Linux, macOS (APFS), FreeBSD 15.0 (UFS), and Windows
Server 2022 (NTFS).

Updates tailscale/corp#51142

Co-authored-by: Sam Wronski <samw@tailscale.com>
Signed-off-by: Brad Fitzpatrick <bradfitz@tailscale.com>
Change-Id: I4771a43e2c9f519b1fd426d7c2b59fa30f30c4c1

@runewake2 runewake2 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants